<template>
	<view>
		<cu-custom bgColor="bg-rgb" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">新增辅料回温</block>
		</cu-custom>
		<form>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.1 + 's'}]">
				<view class="title">辅料SN</view>
				<input :focus="focus_index == 0" placeholder="请输入辅料SN" name="input" type="text" v-model="myFormData.ancillarySn" @confirm="queryByBatchId(myFormData.ancillarySn)"></input>
			</view>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.2 + 's'}]">
				<view class="title">辅料名称</view>
				<input placeholder="请输入辅料名称" :disabled="true" name="input" type="text" v-model="myFormData.ancillaryName"></input>
			</view>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.3 + 's'}]">
				<view class="title">辅料料号</view>
				<input placeholder="请输入辅料料号" :disabled="true" name="input" v-model="myFormData.ancillaryCode"></input>
			</view>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.4 + 's'}]">
				<view class="title">辅料类型</view>
				<input placeholder="请输入辅料类型" :disabled="true" name="input" type="text" v-model="myFormData.ancillaryType"></input>
			</view>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.5 + 's'}]">
				<view class="title">辅料规格</view>
				<input placeholder="请输入辅料规格" :disabled="true" name="input" type="text" v-model="myFormData.ancillaryGague"></input>
			</view>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.7 + 's'}]">
				<view class="title">回温时间(分钟)</view>
				<input placeholder="请输入回温时间(分钟)" :disabled="true" name="input" type="number" v-model="myFormData.heatTime"></input>
			</view>

			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.8 + 's'}]">
				<view class="title">最大回温时间(分钟)</view>
				<input placeholder="请输入最大回温时间(分钟)" :disabled="true" name="input" type="number" v-model="myFormData.maxHeatime"></input>
			</view>
			<view class="cu-form-group animation-slide-bottom" :style="[{animationDelay: 0.9 + 's'}]">
				<view class="title">回温开始时间</view>
				<view class="" @click="stirBegin">
					{{stirBegintimeString}}
				</view>
			</view>
			<view class="padding flex flex-direction animation-slide-bottom" :style="[{animationDelay:1.1 + 's'}]">
				<button class="cu-btn bg-rgb lg" @click="onSubmit">添加</button>
			</view>
		</form>
		<w-picker :visible.sync="visible1" mode="date" startYear="2017" endYear="2229" :value="myFormData.cleanTime" :current="true"
		 fields="second" @confirm="onConfirm($event,'stirBegintime')" :disabled-after="false" ref="date1"></w-picker>
	</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";
	export default {
		components: {
			wPicker
		},
		data() {
			return {
				myFormData: {
					ancillaryCode: '', // 辅料料号
					ancillaryName: '', // 辅料名称
					ancillarySn: '', // 辅料SN
					ancillaryType: '', // 辅料类型
					ancillaryGague: '', // 辅料规格
					heatBegintime: '', //回温开始时间
					operator: uni.getStorageSync('login_user_info').realname, //操作员
					heatTime: '', // 回温时间(分钟)
					maxHeatime: '', // 最大回温时间
				},
				stirBegintimeString: '请选择开始搅拌时间',
				visible1: false,
				focus_index:-1
			};
		},
		onLoad: function(option) {},
		methods: {
			queryByBatchId(id){
				if(id){
					this.$http.get('produce/mesAncillarytoolHeat/queryByBatchId', {
						params: {id:id}
					}).then(res => {
						if(res.data.code === 200){
							this.myFormData.ancillaryName = res.data.result.productName
							this.myFormData.ancillaryCode = res.data.result.materielCode
							this.myFormData.ancillaryType = res.data.result.materielType
							this.myFormData.ancillaryGague = res.data.result.unit
							this.myFormData.heatTime = res.data.result.heatTime
							this.myFormData.maxHeatime = res.data.result.heatTimelimit
						}else {
							// this.$tip.error(res.data.message)
              this.modal.show({ type:'error', content:res.data.message })
						}
					})
				}
			},
			focusNextInput(index) {
				// var inputs = document.getElementsByTagName("input");
				if (index == 0) {
					if (this.myFormData.ancillarySn.replace(/\s+/g, "") == '') {
						// this.$tip.error('辅料SN不能为空')
            this.modal.show({ type:'error', content:'辅料SN不能为空' })
						return
					}
					let data = {
						ancillarySn: this.myFormData.ancillarySn
					}
					this.$http.get('produce/mesChiefdataAncillarytool/list', {
						params: data
					}).then(res => {
						console.log(res)
						if (res.data.success) {
							this.myFormData.ancillaryCode = res.data.result.records[0].ancillaryCode
							this.myFormData.ancillaryName = res.data.result.records[0].ancillaryName
							this.myFormData.ancillaryType = res.data.result.records[0].ancillaryType
							this.myFormData.ancillaryGague = res.data.result.records[0].ancillaryGague
						} else {
							// this.$tip.error(res.data.message)
              this.modal.show({ type:'success', content:res.data.message })
						}
					})
					this.focus_index = Number(index) + 5
					return
				}
				console.log(index,11111111111111)
				if (index == 6) {
					return
				} else {
					this.focus_index = Number(index) + 1
				}
			},
			stirBegin() {
				this.$refs.date1.show()
			},
			onConfirm(e, date) {
				if (date == 'stirBegintime') {
					this.stirBegintimeString = e.value
					this.myFormData.heatBegintime = e.value
				}
			},
			onSubmit() {
				if(!this.myFormData.ancillarySn){
					// return this.$tip.error('请扫描/输入辅料SN!')
          return this.modal.show({ type:'success', content:'请扫描/输入辅料SN!' })
				}
				if(!this.myFormData.heatBegintime){
					// return this.$tip.error('请选择回温开始时间!')
          return this.modal.show({ type:'success', content:'请选择回温开始时间!' })
				}
				uni.showModal({
					title: '辅料回温',
					content: '是否添加？',
					cancelText: '取消',
					confirmText: '添加',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask: true,
								title: '添加中.....'
							})
							if (this.myFormData.ancillarySn.replace(/\s+/g, "") == '') {
								// this.$tip.error('辅料SN不能为空');
								this.modal.show({ type:'error', content:'辅料SN不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.ancillaryName.replace(/\s+/g, "") == '') {
								// this.$tip.error('辅料名称不能为空');
								this.modal.show({ type:'error', content:'辅料名称不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.ancillaryCode.replace(/\s+/g, "") == '') {
								// this.$tip.error('辅料料号不能为空');
								this.modal.show({ type:'error', content:'辅料料号不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.ancillaryType.replace(/\s+/g, "") == '') {
								// this.$tip.error('辅料类型不能为空');
								this.modal.show({ type:'error', content:'辅料类型不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.ancillaryGague.replace(/\s+/g, "") == '') {
								// this.$tip.error('辅料规格不能为空');
								this.modal.show({ type:'error', content:'辅料规格不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.heatTime.replace(/\s+/g, "") == '') {
								// this.$tip.error('回温时间不能为空');
								this.modal.show({ type:'error', content:'回温时间不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.maxHeatime.replace(/\s+/g, "") == '') {
								// this.$tip.error('最大回温时间不能为空');
								this.modal.show({ type:'error', content:'最大回温时间不能为空' })
								uni.hideLoading()
								return false
							} else if (this.myFormData.heatBegintime.replace(/\s+/g, "") == '') {
								// this.$tip.error('回温开始时间不能为空');
								this.modal.show({ type:'error', content:'回温开始时间不能为空' })
								uni.hideLoading()
								return false
							}
							console.log(this.myFormData)
							let mesAncillarytoolHeat = this.myFormData
							this.$http.post('/produce/mesAncillarytoolHeat/add', mesAncillarytoolHeat).then(res => {
								console.log(res)
								uni.hideLoading()
								if (res.data.success) {
									// this.$tip.success(res.data.message);
									this.modal.show({ type:'success', content:res.data.message })
									setTimeout(function() {
										uni.navigateBack({
											delta: 1
										})
									}, 1000);
								} else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.btn {
		background-color: rgb(0, 64, 152);
		padding: 10rpx;
		color: white;
		border-radius: 10rpx;
	}

	input {
		text-align: right;
	}
</style>
